SearchForm =
{
    dates: {},
    init: function()
    {
        var vers = 0;
        if ($.browser.msie) {
            if (typeof $.browser.version != 'undefined') {
                vers = $.browser.version;
                //alert('$.browser.version = ' + vers);
            } else if (typeof $.browser.version.number != 'undefined') {
                vers = $.browser.version.number;
                //alert('$.browser.version.number = ' + vers);
            }
        }
        if (!$.browser.msie || vers > 5.5) {
            var frm = $('#package-filter');
            if(frm.length > 0) {
                $('input[type=text]', frm[0]).each(function() {
                    if(this.nodeName.toLowerCase() == 'input' && (this.id == 'depdate' || this.id == 'enddate')) {
                        $(this).bind('focus', function() {
                            $(this).next('a.dp-choose-date').click();
                            $(this).blur();
                        });
                    }
                });
                $('input#setFilter').click(function(e) {
                    e.preventDefault();
                    e.stopPropagation();
                    $('#package-filter').get(0).submit();
                    return true;
                    //$(this).parent().parent().submit();
                });
                $('a#clearFilter').bind('click', SearchForm.clearFilterData);
            }
        }
    },
    dateSelected: function()
    {
        SearchForm.dates[this.id] = $(this).val();
        if (this.id == 'depdate' && typeof SearchForm.dates[this.id] != 'undefined') {
            var date = Date.fromString(this.value).addDays(7).asString();
            var enddate = $("#enddate");
            SearchForm.dates.enddate = date;
            enddate.val(date).dpSetSelected(date).dpSetStartDate(this.value);
        }
        var dateObj = $('#' + this.id);
        dateObj.dpSetSelected(SearchForm.dates[this.id]);
    },
    clearFilterData: function(e)
    {
        e.preventDefault();
        var clearAnchor = $(e.target);
        var frm = $('#package-filter');
        $('input', frm[0]).each(function() {
            var type = $(this).attr('type');
            if (type.toLowerCase() == 'text') {
                //$(this).removeAttr('value');
                $(this).val('');
            } else if (type.toLowerCase() == 'checkbox') {
                this.checked = true;
            }
        });
        $('select', frm[0]).each(function() {
            this.selectedIndex = 0;
        });
        SearchForm.dates = {};
        $(clearAnchor).blur();
    }
};

$(SearchForm.init);

$(function() {
    $('.date-picker').each(function() {
//        $(this).datePicker();
        SearchForm.dates[this.id] = $(this).val();

        if ($('#package-filter').size()) {
            $(this).bind('dateSelected', SearchForm.dateSelected);
        }

        if (SearchForm.dates[this.id].length > 0) {
            $(this).dpSetSelected(SearchForm.dates[this.id]);
        }
    });

    if (SearchForm.dates.depdate) {
        var enddate = $("#enddate");
        enddate.dpSetStartDate(SearchForm.dates.depdate);
    }

    /*$('input[type="image"][alt="Очистить"]').click(function(e) {
        e.preventDefault();
        SearchForm.clearForm();
    });*/

    /*$('#package-filter').bind('submit', function() {
        debugger;
    });*/
});
